package com.xy.tool.util;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import java.io.*;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.Base64;
import java.util.HashMap;

/**
 * @author Greekn Zhang
 * @date 2021/8/11 17:57
 * @description
 */
public class QRCodeGenerateUtils {

    private static final String BASE64 = "data:image/png;base64,";

    public static String toBase64(byte[] bytes) {
        return BASE64 + Base64.getEncoder().encodeToString(bytes);
    }

    /**
     * @author Greekn Zhang
     * @date 2021/8/11 18:05
     * 生成二维码返回byte[]
     * @param text 文本
     * @param width 二维码宽度
     * @param height 二维码高度
     */
    public static byte[] generate(String text, Integer width, Integer height) throws WriterException, IOException {
        //根据文本生成二维码
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        HashMap<EncodeHintType, Object> hints = new HashMap<>(2);
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width == null ? 300 : width, height == null ? 300 : height, hints);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(bitMatrix, "png", out);
        return out.toByteArray();
    }

    /**
     * @author Greekn Zhang
     * @date 2021/8/11 18:05
     * 生成二维码到指定路径
     * @param text 文本
     * @param width 二维码宽度
     * @param height 二维码高度
     * @param filePath 图片存放地址
     */
    public static void generate(String text, Integer width, Integer height, String filePath) throws WriterException, IOException {
        //根据文本生成二维码
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        HashMap<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width == null ? 300 : width, height == null ? 300 : height, hints);
        //指定图像生成后保存路径并指定名字
        Path path = FileSystems.getDefault().getPath(filePath);
        MatrixToImageWriter.writeToPath(bitMatrix, "png", path);
    }


    /**
     * @author Greekn Zhang
     * @date 2021/8/11 18:05
     * 生成条码到指定路径
     * @param text 文本
     * @param width 条码宽度
     * @param filePath 图片存放地址
     */
    public static void generateBarCode(String text, Integer width, String filePath) throws WriterException, IOException {
        double height = width * 0.3;
        HashMap<EncodeHintType, Object> hints = new HashMap<>(2);
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.CODE_128, width, (int) height, hints);
        //指定图像生成后保存路径并指定名字
        Path path = FileSystems.getDefault().getPath(filePath);
        MatrixToImageWriter.writeToPath(bitMatrix, "png", path);
    }

    /**
     * @author Greekn Zhang
     * @date 2021/8/11 18:05
     * 生成条码返回byte[]
     * @param text 文本
     * @param width 条码宽度
     */
    public static byte[] generateBarCode(String text, Integer width) throws WriterException, IOException {
        double height = width * 0.3;
        HashMap<EncodeHintType, Object> hints = new HashMap<>(2);
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.CODE_39, width, (int) height, hints);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(bitMatrix, "png", out);
        return out.toByteArray();
    }

    public static byte[] readStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        outStream.close();
        inStream.close();
        return outStream.toByteArray();
    }

    public static void main(String[] args) throws IOException, WriterException {
        String path = "D:\\barcode.png";
        generateBarCode("hello-world-123", 400, path);
    }

}
